package nl.haemers.form.types
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	
	import nl.haemers.base.BaseSprite;
	import nl.haemers.base.BaseMovieClip;
	import nl.haemers.form.rules.IRule;
	
	/**
	 * @author Jonathan Haemers
	*/
	public class FormTextField extends BaseSprite implements IFormField
	{
		protected var _valid				:Boolean;
		protected var _required				:Boolean;
		protected var _formFieldName		:String;
		protected var _fieldValue			:String;
		protected var _title				:String;
		protected var _rules 				:Vector.<IRule>;
		
		protected var _minLength			:int;
		protected var _maxLength			:int;
		
		public var txtField					:TextField;
		
		public function FormTextField() 
		{
			super();
		}
		
		/** Public methods **/
		public function validate() : Boolean
		{
			_valid = true;
			
			if (!fieldEmpty(txtField.text) && txtField.text != _title)
			{
				var rule:IRule;
				for each(rule in _rules)
				{
					_valid = rule.validate(txtField.text);
					if (!_valid)
					{
						break;
					}
				}
			}
			else if(_required)
			{
				_valid = false;
			}
			
			
			if (_maxLength)
			{
				if (txtField.length > _maxLength)
				{
					_valid = false;
				}
			}
			
			if (_minLength)
			{
				if (txtField.length < _minLength)
				{
					_valid = false;
				}
			}
			
			updateState();
			
			return _valid;
		}
		
		public function addRule(rule:IRule) : void
		{
			if (!_rules)
			{
				_rules = new Vector.<IRule>();
			}
			
			_rules.push(rule);
		}
		
		/** Getters & Setters **/
		public function get valid() : Boolean
		{ 
			return _valid;
		}
		public function set valid(value:Boolean) : void 
		{
			_valid = value;
			if (stage)
			{
				updateState();
			}
		}
		
		public function set rules(value:Vector.<IRule>) : void
		{
			_rules = Vector.<IRule>;
		}
		
		public function get required() : Boolean
		{ 
			return _required;
		}
		public function set required(value:Boolean) : void 
		{
			_required = value;
		}
		
		public function get minLength() : int
		{ 
			return _minLength;
		}
		public function set minLength(value:int) : void 
		{
			_minLength = value;
		}
		
		public function get maxLength() : int
		{ 
			return _maxLength;
		}
		public function set maxLength(value:int) : void 
		{
			_maxLength = value;
			txtField.maxChars = _maxLength;
		}
		
		public function get fieldValue() : String
		{ 
			return _fieldValue;
		}
		public function set fieldValue(value:String) : void 
		{
			_fieldValue = value;
		}
		
		public function get formFieldName() : String
		{ 
			return _formFieldName;
		}
		public function set formFieldName(value:String) : void 
		{
			_formFieldName = value;
		}
		
		public function get title() : String
		{ 
			return _title;
		}
		public function set title(value:String) : void 
		{
			_title = value;
		}
		
		override public function get tabIndex() : int
		{ 
			return txtField.tabIndex;
		}
		override public function set tabIndex(value:int) : void 
		{
			txtField.tabIndex = value;
		}
		
		/** Protected methods **/
		override protected function init(e:Event = null) : void
		{
			super.init(e);
			startup();
		}
		
		protected function startup() : void
		{
			if (txtField)
			{
				configListeners(txtField);
				if (_title)
				{
					txtField.text = _title;
				}
				else
				{
					_title = txtField.text;
				}
			}
		}
		
		protected function updateState() : void
		{
			if (_valid && !fieldEmpty(txtField.text) && txtField.text != _title)
			{
				_fieldValue = txtField.text;
			}
		}
		
		protected function configListeners(d:EventDispatcher) : void
		{
			d.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			d.addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
		}
		
		protected function focusInHandler(e:FocusEvent) : void
		{
			if (txtField.text == _title)
			{
				txtField.text = "";
			}
		}
		
		protected function focusOutHandler(e:FocusEvent) : void
		{
			if (fieldEmpty(txtField.text))
			{
				txtField.text = _title;
				_fieldValue = "";
			}
			else
			{
				validate();
			}
		}
		
		protected function fieldEmpty(value:String) : Boolean
		{
			return (trim(value) == "");
		}
		
		/**FormField funtions**/
		protected function trim(value:String) : String
		{
			return value.replace(/ /g, "");
		}
	}

}